<?php

class Gallery extends Controller {

	function Gallery()
	{
		parent::Controller();	
        if(!admin_session_check()) redirect('admin/login');
        if(!is_allowed("all")) show_404();
        $this->load->model('gallerymodel');  
        $this->load->library("form_validation");       
	}     
    
    function index()
    {

        $data["images"] = $this->gallerymodel->admin_get_list();
        $data["response"] = get_response();  

        $this->load->view('admin/default/head');  
        $this->load->view('admin/default/header', array('selected'=>'pages'));  
        $this->load->view("admin/default/nav_pages", array('selected' => 'gallery'));
        $this->load->view('admin/gallery/gallery_list', $data);   
        $this->load->view('admin/default/footer');   

    }
	  
    function add()
    {
        $this->_process_form();

        $data["action"] = "create";
        $data["token"] = generate_token("gallery",720);

        $this->load->view('admin/default/head');  
        $this->load->view('admin/default/header', array('selected'=>'pages'));  
        $this->load->view("admin/default/nav_pages", array('selected' => 'gallery'));
        $this->load->view('admin/gallery/gallery_form', $data);   
        $this->load->view('admin/default/footer');   

    }            
    function remove($id = false){

        if($id == NULL) show_404();  

        $data["item"] = $this->gallerymodel->admin_get_item($id);

        if(empty($data["item"])){

            set_response('error', "The image you're attempting to delete does not exist");
            redirect('admin/gallery');

        } else {

            $data["controller"] = "admin/gallery";

            $this->load->view('admin/default/head');          
            $this->load->view('admin/default/header', array('selected'=>'pages'));  
            $this->load->view("admin/default/nav_pages", array('selected' => 'gallery'));
            $this->load->view('admin/gallery/gallery_delete', $data);   
            $this->load->view('admin/default/footer');   

        }
    }
    function confirm_removal($id = false){

        if($id == NULL) show_404();  

        $data["item"] = $this->gallerymodel->admin_get_item($id);

        if(empty($data["item"])){

            set_response('error', "The image you're attempting to delete does not exist");
            redirect('admin/gallery');

        } else {

            $this->gallerymodel->admin_delete($id);
            set_response('success', "The image <i>".$data["item"]["Image_Gallery"]."</i> was deleted.");
            redirect('admin/gallery');
        }
    }
    function deactivate($id = false){

        if($id == NULL) show_404();  
        $data = array('Active_Gallery' => 0);
        $this->gallerymodel->admin_update($id, $data);
        $item = $this->gallerymodel->admin_get_item($id);
        set_response('success', "The installation <i>".$item["Name_Gallery"].'</i> is now inactive');
        redirect('admin/gallery');
    }
    function activate($id = false){

        if($id == NULL) show_404();  
        $data = array('Active_Gallery' => 1);
        $this->gallerymodel->admin_update($id, $data);
        $item = $this->gallerymodel->admin_get_item($id);
        set_response('success', "The installation <i>".$item["Name_Gallery"].'</i> is now active');
        redirect('admin/gallery');
    }     
    function _process_form()
    {        
        if($this->input->post("submit_action") !== FALSE){

            if ($this->_validate_form()) {

                if (validate_token("gallery",$this->input->post("token",TRUE))){

                    $data = array(
                        'Image_Gallery' => $this->input->post("image")
                    );

                    $new_id = $this->gallerymodel->admin_insert($data);
                    set_response('success','Image <i>'.$data['Image_Gallery'].'</i> was added to the gallery');
					redirect("admin/gallery");

                }
            }   
        }
    }

    private function _validate_form(){
        
        $config = array(
                        array(
                         'field'   => 'image',
                         'label'   => 'Image',
                         'rules'   => 'trim|required|xss_clean'
                        )
                    );
                                                 
        $this->form_validation->set_rules($config);       
        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
        
        if ($this->form_validation->run($this) == FALSE){
            return FALSE;
        }else
            return TRUE;

    }

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */